package com.zzl.myblog;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.ycl.tabview.library.TabView;
import com.ycl.tabview.library.TabViewChild;
import com.zzl.myblog.Utils.DateUtil;
import com.zzl.myblog.Utils.SqliteUtils;
import com.zzl.myblog.View.Fragment.MainFragment;
import com.zzl.myblog.View.Fragment.TagsFragment;
import com.zzl.myblog.View.Fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    static String TAG= "MainActivity";

    private long lastClickBackTime = System.currentTimeMillis()- 3000; // 回退三秒时刻

    // 获取tabview
    @BindView(R.id.tabView)
    TabView tabView;

    // tabView 的数据集
    List<TabViewChild> tabViewChildList = new ArrayList<>();

    static String username;//账号

    // 四个Fragment
    private MainFragment mainFragment;//首页
    private TagsFragment tagsFragment;//导向
    private UserFragment userFragment;//我的

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定处理初始化 ButterKnife
        ButterKnife.bind(this);

        //初始化 时间工具类单例对象
        DateUtil.getInstance();
        // 初始化 SqliteUtil单例对象，并创建数据库
        SqliteUtils.getInstance();

        //接受 LoginActivity 传来的 用户名和密码
        Intent intent=getIntent();
        username =intent.getStringExtra("username");

        /*
         * TabViewChild(int imageViewSelIcon, int imageViewUnSelIcon, String textViewText, Fragment mFragment)
         * 地址：https://github.com/yaochangliang159/Android-TabView
         * - mFragment：导航栏上面的某一个tab对应的Fragment对象，传进来就可以
         */
        if (mainFragment == null) {
            mainFragment = MainFragment.newInstance(username);
            // 将对应的fragment加入到tabView里
            tabViewChildList.add(new TabViewChild(R.mipmap.main, R.mipmap.main_no, "首页", mainFragment));
        }
        if (tagsFragment == null) {
            tagsFragment = TagsFragment.newInstance("leadFragment", username);
            tabViewChildList.add(new TabViewChild(R.mipmap.lead, R.mipmap.lead_no, "导向", tagsFragment));
        }
//        if (projectFragment == null) {
//            // newInstance(String FragmentKind,String username)
//            //projectFragment = TagsFragment.newInstance("projectFragment", username);
//            //tabViewChildList.add(new TabViewChild(R.mipmap.project, R.mipmap.project_no, "项目", projectFragment));
//        }
        if (userFragment == null) {
            userFragment = UserFragment.newInstance(username);
            tabViewChildList.add(new TabViewChild(R.mipmap.user, R.mipmap.user_no, "我的", userFragment));
        }

        // 为TabView设置数据源
        tabView.setTabViewChild(tabViewChildList, getSupportFragmentManager());

    }

    /**
     * 两次back，退出app
     *
     * */
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastClickBackTime > 2000) { // 后退阻断
            Toast.makeText(this, "再点一次退出", Toast.LENGTH_LONG).show();
            lastClickBackTime = System.currentTimeMillis();
        } else {
            // 1. 杀死进程，释放资源
            // System.exit(0);
            // 2. 保留进程，推向后台，不释放资源
             finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}